const axios = require('axios');
const Q = require('q');
const qs = require('querystring');
// const menudata = require( 'api/menus');       //  接口js引用
// const md5 = require('js-md5');                    //   md5加密
// const b64 = require('js-base64');                //   664加密
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
// axios.defaults.withCredentials = true;
// console.log( axios.defaults.xhrFields)
// * Mocking client-server processing
module.exports = {
    username:'', //  用户名称
    usertime:'',//   用户登录时间
    authorization:'',   // headr 公共的 接口请求需要  md5(  b64（data.sign + (data.userId + data.level) + data.time));
    // posturl:'http://127.0.0.1:10045/',
  // posturl:'http://192.168.19.178:10045/',
  //   posturl:'http://47.96.67.141:10045/',
    posturl:'http://glmp.hzgolong.com:10045/',

    // posturl:'http:'+localStorage.getItem('ipUrl')+':10045/', // 正式环境

    // posturl1:'http://120.55.242.214:97/',
    // 时间格式化
    formatData(d){
        var d1 = new Date();
        if(d != undefined){
            d1 = new Date(d);
        }
        var m = d1.getMonth()+1;
        var d = d1.getDate();
        console.log((d1.getMonth()+'').length+'=========');
        if((d1.getMonth()+'').length <= 1){
            m = '0'+m;
        }
        if((d1.getDate()+'').length <= 1){
            d = '0'+d1.getDate();
        }
        console.log(m + '-' + d + '-' + d1.getFullYear() + ' ' + d1.getHours() + ':' + d1.getMinutes());
        return m + '-' + d + '-' + d1.getFullYear() + ' ' + d1.getHours() + ':' + d1.getMinutes();
    },
    glLogin:function(data){
        var deferred = Q.defer();
        console.log('登录--------'+this.posturl+'user/login');
        axios({
            method:'POST',
            url: this.posturl+'user/login',
            data:data
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },

    // 登录
    insuranceLogin:function(data){
        var deferred = Q.defer();
        axios({
            method:'POST',
            url: 'http://192.168.0.114:9988/api/SysInfo/UserLogin?UserName='+data.loginAccount+'&UserPass='+data.loginPassword,
         //   headers:{'Authorization':'','Content-Type':'application/json;charset=utf-8'},
         //   params:{ "UserName":data.loginAccount,'UserPass':data.loginPassword},
    }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },
    //  客户  列表
    getList:function(data){
        var deferred = Q.defer();
        axios({
            method:'POST',
            headers:{'Authorization':this.authorization,'Content-Type':'application/json;charset=utf-8'},
            url: 'http://114.55.56.242:8090/getdownresult',
            params:{key:1}
        }).then(function (response) {
            console.log(response)
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error);
            console.log(error)
        });
        return deferred.promise;
    },

    // 城市列表
    insuranceCityList:function(data){
        var deferred = Q.defer();
        axios({
            method:'POST',
            url: 'http://192.168.0.114:9894/api/SysInfo/GetCityModel',
            headers:{'Authorization':'','Content-Type':'application/json;charset=utf-8'},
           data:{ "FLD_AREACODE":'330100'},
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log('错误打印')
            console.log(error)
        });
        return deferred.promise;
    },

    // 退出
    insurancetuichu:function(){
        var deferred = Q.defer();
        axios({
            method:'POST',
            url: this.u + 'open/logoutValidate',
            responseType: 'json',
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },
    // 修改密码
    changePwd:function(data){
        var deferred = Q.defer();
        axios({
            method:'POST',
            url: this.u + 'api/modifyPassword',
            data:{newPassword:md5(data.newPassword),oldPassword:md5(data.oldPassword)}
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },

    userData:function(data){
        var deferred = Q.defer();
        axios({
            method:'POST',
            headers:{'Authorization':this.authorization,'Content-Type':'application/json;charset=utf-8'},
            url: this.u + 'api/obtainUser',
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },

    //  订单统计
    orderCountList:function(data){
        var deferred = Q.defer();
        axios({
            method:'POST',
            url: this.u+ 'api/agent/statistics',
            params :data
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },
};

